//
//  YWPhotoBrowserCell.m
//  Chinese
//
//  Created by yellow on 2021/3/8.
//  Copyright © 2021 YSLC. All rights reserved.
//

#import "YWPhotoBrowserCell.h"
#import "YWProgressView.h"

@interface YWPhotoBrowserCell ()<UIScrollViewDelegate>

@property(nonatomic,weak)YWProgressView *progressView;

@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation YWPhotoBrowserCell

-(void)setupImgWithLittleUrlString:(NSString *)littleUrlString bigUrlString:(NSString *)bigUrlString{
    
    self.littleUrlString = littleUrlString;
    self.bigUrlString = bigUrlString;
    
    UIImage *littleImage = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:littleUrlString];
    CGFloat height = 0;
    if (littleImage == nil) { //还没缓存到littleImage、默认是正方形的frame
        // 3.计算imageView的frame
        CGFloat width = Wi;
        height = width;
        CGFloat y = 0;
        
        y = (He - height)*0.5;
      
        self.imageView.frame = CGRectMake(0, y, width, height);
    }
    else{
        
        // 3.计算imageView的frame
        CGFloat width = Wi;
        height = width / littleImage.size.width * littleImage.size.height;
        CGFloat y = 0;
        
        if (height > He) {
            y = 0;
        }
        else{
            y = (He - height)*0.5;
        }
      
        self.imageView.frame = CGRectMake(0, y, width, height);
        
      
      
    }
    
    
    
    // 4.设置imagView的图片
//    self.progressView.hidden = NO;
    
    // 5.设置scrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(0, height);
    
    
    MJWeakSelf
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:bigUrlString] placeholderImage:littleImage options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            weakSelf.progressView.hidden = NO;
            weakSelf.progressView.progress = receivedSize*1.0/expectedSize;
        });
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
//        weakSelf.progressView.hidden = YES;
//        weakSelf.imageView.image = image;
        
        weakSelf.progressView.hidden = YES;
        
        //这里还要继续计算大图的宽高来决定imageView的frame
        CGFloat width = Wi;
        CGFloat height = width / image.size.width * image.size.height;
        CGFloat y = 0;
        
        if (height > He) {
            y = 0;
        }
        else{
            y = (He - height)*0.5;
        }
      
        weakSelf.imageView.frame = CGRectMake(0, y, width, height);
        
        
       //还要再设置多一次scrollView.contentSize
        weakSelf.scrollView.contentSize = CGSizeMake(0, height);
    }];
    
    
    
    
}



-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = self.contentView.bounds;
        _scrollView.width -= 20;
        _scrollView.delegate = self;
        _scrollView.minimumZoomScale = 1;
        _scrollView.maximumZoomScale = 5.0;
        
    }
    return _scrollView;
}


-(UIImageView *)imageView{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick)];
        [_imageView addGestureRecognizer:tap];
        _imageView.userInteractionEnabled = YES;
        
    }
    return _imageView;
}
-(void)imageViewClick{
    
    if ([self.delegate respondsToSelector:@selector(photoBrowserCell:didTapWithUrl:)]) {
        [self.delegate photoBrowserCell:self didTapWithUrl:self.littleUrlString];
    }
    
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.scrollView];
//        [self.contentView addSubview:self.progressView];
        [self.scrollView addSubview:self.imageView];
        
        YWProgressView *progressView = [[YWProgressView alloc] init];
        self.progressView = progressView;
        progressView.bounds = CGRectMake(0, 0, 50, 50);
        progressView.center = CGPointMake(Wi * 0.5, He * 0.5);
        [self.contentView addSubview:progressView];
        
        
    }
    return self;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}


@end
